package net.goutalk.glcs.module.app.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import net.goutalk.glcs.common.annotation.GlcsLog;
import net.goutalk.glcs.common.constant.GlobalConstant;
import net.goutalk.glcs.common.enums.EnabledMark;
import net.goutalk.glcs.common.model.result.R;
import net.goutalk.glcs.common.page.ConventPage;
import net.goutalk.glcs.common.page.PageOutput;
import net.goutalk.glcs.common.utils.VoToColumnUtil;
import net.goutalk.glcs.module.app.entity.AppMenu;
import net.goutalk.glcs.module.app.service.IAppMenuService;
import net.goutalk.glcs.module.app.vo.AppMenuListVo;
import net.goutalk.glcs.module.app.vo.AppMenuPageVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import net.goutalk.glcs.module.app.dto.AddAppMenuDto;
import net.goutalk.glcs.module.app.dto.AppMenuListDto;
import net.goutalk.glcs.module.app.dto.AppMenuPageDto;
import net.goutalk.glcs.module.app.dto.UpdateAppMenuDto;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  app 菜单前端控制器
 * </p>
 *
 * @author tanyujie
 * @since 2023-07-26
 */
@RestController
@RequestMapping(GlobalConstant.APP_MODEL +"/menu")
@Api(value = GlobalConstant.APP_MODEL +"/menu", tags = "app 菜单模块")
@AllArgsConstructor
public class AppMenuController {

    private final IAppMenuService appMenuService;

    @GetMapping("/page")
    @ApiOperation("app菜单分页接口")
    public R page(AppMenuPageDto dto){

        LambdaQueryWrapper<AppMenu> queryWrapper = Wrappers.lambdaQuery(AppMenu.class)
                .eq(StrUtil.isNotBlank(dto.getName()), AppMenu::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getCode()), AppMenu::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getCategoryId()), AppMenu::getCategoryId, dto.getCategoryId())
                .orderByAsc(AppMenu::getSortCode)
                .select(AppMenu.class, x -> VoToColumnUtil.fieldsToColumns(AppMenuPageVo.class).contains(x.getProperty()));

        IPage<AppMenu> page = appMenuService.page(ConventPage.getPage(dto), queryWrapper);
        PageOutput<AppMenuPageVo> pageOutput = ConventPage.getPageOutput(page, AppMenuPageVo.class);
        return R.ok(pageOutput);
    }


    @GetMapping("/list")
    @ApiOperation("app菜单 列表 不分页")
    public R list(AppMenuListDto dto){

        LambdaQueryWrapper<AppMenu> queryWrapper = Wrappers.lambdaQuery(AppMenu.class)
                .eq(StrUtil.isNotBlank(dto.getName()), AppMenu::getName, dto.getName())
                .eq(StrUtil.isNotBlank(dto.getCode()), AppMenu::getCode, dto.getCode())
                .eq(ObjectUtil.isNotNull(dto.getCategoryId()), AppMenu::getCategoryId, dto.getCategoryId())
                .eq(AppMenu::getEnabledMark, EnabledMark.ENABLED.getCode())
                .orderByAsc(AppMenu::getSortCode)
                .select(AppMenu.class, x -> VoToColumnUtil.fieldsToColumns(AppMenuPageVo.class).contains(x.getProperty()));


        List<AppMenu> list = appMenuService.list(queryWrapper);

        List<AppMenuListVo> appMenuListVos = BeanUtil.copyToList(list, AppMenuListVo.class);

        return R.ok(appMenuListVos);
    }

    @PostMapping
    @ApiOperation("新增")
    public R add(@Valid @RequestBody AddAppMenuDto dto){

        AppMenu appMenu = BeanUtil.toBean(dto, AppMenu.class);

        return R.ok(appMenuService.save(appMenu));

    }


    @PutMapping
    @ApiOperation("修改")
    public R update(@Valid @RequestBody UpdateAppMenuDto dto){

        AppMenu appMenu = BeanUtil.toBean(dto, AppMenu.class);

        return R.ok(appMenuService.updateById(appMenu));

    }

    @DeleteMapping
    @ApiOperation(value = "删除")
    @GlcsLog(value = "删除app端展示页面设计")
    public R delete(@Valid @RequestBody List<Long> ids) {
        appMenuService.removeBatchByIds(ids);
        return R.ok(Boolean.TRUE);
    }
}
